package com.skyz.base.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.toast.ToastUtils;
import com.skyz.base.adapter.BaseRecyclerViewAdapter;
import com.skyz.base.adapter.EnvironmentAdapter;
import com.skyz.base.R;
import com.skyz.base.manager.EnvironmentManager;
import com.skyz.base.manager.SystemManager;

/**
 * 选择环境弹窗
 */
public class EnvironmentDialogFragment extends BaseDialogFragment {

    public static void showDialogFragment(@NonNull FragmentManager manager, @Nullable String tag) {
        EnvironmentDialogFragment environmentDialogFragment = new EnvironmentDialogFragment();
        environmentDialogFragment.show(manager, tag);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fragment_environment;
    }

    @Override
    protected void onLayoutInflated(View view) {
        SwitchCompat swcNetLog = view.findViewById(R.id.swc_net_log);
        TextView tvCurrentEnvironment = view.findViewById(R.id.tv_current_environment);
        swcNetLog.setChecked(SystemManager.getInstance().getNetLogToFile());
        swcNetLog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SystemManager.getInstance().setNetLogToFile(isChecked);
                ToastUtils.show((isChecked ? "开启" : "关闭") + getString(R.string.网络日志));
            }
        });


        tvCurrentEnvironment.setText(EnvironmentManager.getInstance().getCheckedEnvironment().getName());

        RecyclerView rvEnvironment = view.findViewById(R.id.rv_environment);
        rvEnvironment.setLayoutManager(new LinearLayoutManager(mContext));
        EnvironmentAdapter environmentAdapter = new EnvironmentAdapter();
        rvEnvironment.setAdapter(environmentAdapter);
        environmentAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<EnvironmentManager.Environment>() {
            @Override
            public void onItemClick(EnvironmentManager.Environment environment, int position) {
                EnvironmentManager.getInstance().setCheckedEnvironmentIndex(position);
                dismiss();
                if (environment == null) {
                    return;
                }
                String name = environment.getName();
                ToastUtils.show(getString(R.string.当前环境) + name);
            }
        });
        environmentAdapter.addDataList(EnvironmentManager.getInstance().getEnvironmentList());
    }

    @Override
    protected WindowManager.LayoutParams initLayoutParams(WindowManager.LayoutParams layoutParams) {
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        return layoutParams;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }
}
